<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class uindex extends CI_Controller {
	
	  function __construct() {
        parent::__construct();
        $this->load->model("mtruong");
        $this->load->model("mtinhthanh");
    }
	public function index()
	{
		$data =array();
		$data['truong'] = $this->mtruong->gets();
		
		$this->load->view('nv1', $data);
		
	}
	
	//action
	function xephangnv1($truongCode = '', $diem =''){
	
	
	}
	
	//tìm điểm chuẩn(nv1), tìm điểm nv2, nv3
	function diemnguyenvong($nv = 'nv1'){
		$data = array();
		$truong = $this->input->post("truong");
		$arr_data = array();
		if($truong){
			$this->db->where("TRUONG_MA",$truong);
			$this->db->from("nganh");
			$kq = $this->db->get()->result();
			//echo $this->db->last_query();
			
			//thông tin trường
			$truong = $this->mtruong->get($truong,'TRUONG_MA');
			$arr_data[0]['title'] = @$truong->TRUONG_TEN;
			
			if($kq){
			$i = 1;
				foreach($kq as $r){
					$arr_data[$i] = array(
						'manganh' =>$r->NGANH_MA,
						'tennganh' =>$r->NGANH_TEN,
						'khoi' =>$r->NGANH_KHOI,
						'diemchuan' =>$r->NGANH_DIEM1,
						'ghichu' =>($r->NGANH_GHICHU)?$r->NGANH_GHICHU:'',
						);
					if($nv =='nv2')
						$arr_data[$i]['diemchuan'] = $r->NGANH_DIEM2;
					if($nv =='nv3')
						$arr_data[$i]['diemchuan'] = $r->NGANH_DIEM3;
					$i++;
				}
			
			}
			die(json_encode($arr_data));
		}
		$data =array();
		if($nv =='nv2')
			$data['urlSearch'] = base_url().'diem-nv2.html';
		else if($nv =='nv3')
			$data['urlSearch'] = base_url().'diem-nv3.html';
		else
			$data['urlSearch'] = base_url().'diem-chuan.html';
		$data['truong'] = $this->mtruong->gets();
		
		$this->load->view('diemnguyenvong', $data);
	}
	function ketquatimnguyenvong($nv = 'nv2',$matruong = '',$khoithi= '',$diem =''){
		$data = array();
		if($nv == 'nv2')
			$this->db->where("NGANH_DIEM2 <=",$diem);
		else
			$this->db->where("NGANH_DIEM3 <=",$diem);
		$this->db->where("TRUONG_MA",$matruong);
		$this->db->like("NGANH_KHOI",$khoithi);
		$this->db->from("nganh");
		$data['data'] = $this->db->get()->result();
		if(count($data['data'])>0)
		for($i=0; $i<count($data['data']); $i++){
			if($nv =='nv2'){
				$data['data'][$i]->NGANH_DIEM = $data['data'][$i]->NGANH_DIEM2;
			}
			else {
				$data['data'][$i]->NGANH_DIEM = $data['data'][$i]->NGANH_DIEM3;
			}
		}
		$data['linkBack'] = base_url().'tim-nguyen-vong/'.$nv.'.html';
		$data['matruong'] = $matruong;
		$this->load->view('ketquatuvanchontruong', $data);
	}
	
	function ketquatuvanchontruong($matruong = '',$khoithi= '',$diem =''){
		$data = array();
		$data['linkBack'] = base_url().'tu-van-chon-truong.html';
		$this->db->where("NGANH_DIEM1 <=",$diem);
		$this->db->where("TRUONG_MA",$matruong);
		$this->db->like("NGANH_KHOI",$khoithi);
		$this->db->from("nganh2013");
		$data['data'] = $this->db->get()->result();
		if(count($data['data'])>0)
		for($i=0; $i<count($data['data']); $i++){
			$data['data'][$i]->NGANH_DIEM = $data['data'][$i]->NGANH_DIEM1;
		}
		$data['matruong'] = $matruong;
		$this->load->view('ketquatuvanchontruong', $data);
	}
	
	function tuvanchontruong(){
		$data =array();
		$post = $this->input->post();
		$arr_data = array();
		
		if($post){
			$tongdiem = $this->input->post("tongdiem");
			$khoithi = $this->input->post("khoithi");
			$tinhthanh = $this->input->post("tinhthanh");
			$this->db->where("NGANH_DIEM1 <=",$tongdiem);
			if($tinhthanh >0)
				$this->db->where("TINH_MA",$tinhthanh);
			$this->db->like("NGANH_KHOI",$khoithi);
			$this->db->from("nganh2013 n");
			$this->db->join("truong","n.TRUONG_MA = truong.TRUONG_MA");
			$this->db->group_by("truong.TRUONG_MA");
			$this->db->order_by("NGANH_DIEM1 desc");
			$result = $this->db->get()->result();
			foreach($result as $r){
				$arr_data[] = array(
					'matruong' =>$r->TRUONG_MA,
					'tentruong' =>'<a href="'.base_url().'tu-van-chon-truong/'.$r->TRUONG_MA.'/'.$khoithi.'/'.$tongdiem.'.html">'.$r->TRUONG_TEN.'</a>',
					'manganh' =>$r->NGANH_MA,
					'khoithi' =>$r->NGANH_KHOI,
					'diem' =>$r->NGANH_DIEM1,
				);
			}
		die(json_encode($arr_data));
		return;
		}
		$data['urlSearch'] = base_url().'tu-van-chon-truong.html';
		$data['tinhthanh'] = $this->mtinhthanh->gets();
		$this->load->view('tuvanchontruong', $data);
	
	}
	
	//tìm nguyện vọng năm 2014
	function timnguyenvong($nv = 'nv2'){
		$data =array();
		$post = $this->input->post();
		$arr_data = array();
		if($post){
			$tongdiem = $this->input->post("tongdiem");
			$khoithi = $this->input->post("khoithi");
			$tinhthanh = $this->input->post("tinhthanh");
			if($nv == 'nv2')
				$this->db->where("NGANH_DIEM2 <=",$tongdiem);
			else
				$this->db->where("NGANH_DIEM3 <=",$tongdiem);
			//else ($nv == 'nv3')
				//$this->db->where("NGANH_DIEM1 <=",$tongdiem);
			if($tinhthanh >0)
				$this->db->where("TINH_MA",$tinhthanh);
			$this->db->like("NGANH_KHOI",$khoithi);
			$this->db->from("nganh");
			$this->db->join("truong","nganh.TRUONG_MA = truong.TRUONG_MA");
			$this->db->group_by("truong.TRUONG_MA");
			$this->db->order_by("NGANH_DIEM1 desc");
			$result = $this->db->get()->result();
			foreach($result as $r){
				$arr_data[] = array(
					'matruong' =>$r->TRUONG_MA,
					'tentruong' =>'<a href="'.base_url().'tim-nguyen-vong/'.$nv.'/'.$r->TRUONG_MA.'/'.$khoithi.'/'.$tongdiem.'.html">'.$r->TRUONG_TEN.'</a>',
					'manganh' =>$r->NGANH_MA,
					'khoithi' =>$r->NGANH_KHOI,
					'diem' =>$r->NGANH_DIEM1,
				);
			}
		die(json_encode($arr_data));
		return;
		}
		$data['urlSearch'] = base_url().'tim-nguyen-vong/'.$nv.'.html';
		$data['tinhthanh'] = $this->mtinhthanh->gets();
		$this->load->view('tuvanchontruong', $data);
	}
	
	//search điểm nguyện vọng 1
	function tradiem($nv = 'nv1'){
		$truong = $this->input->post("truong");
		$ten = $this->input->post("keyword");
		$arr_data = array();
		if($truong && $ten){
			$this->db->where("NV_MA_TRUONG",$truong);
			$this->db->like('NV_HO_TEN', $ten, 'before'); 
			$this->db->from("nv1");
			$kq = $this->db->get()->result();
			
			if(!$kq){
				$this->db->where("NV_MA_TRUONG",$truong);
				$this->db->where('NV_SO_BAODANH', $ten); 
				$this->db->from("nv1");
				$kq = $this->db->get()->result();
			}
			//echo $this->db->last_query();
			//thông tin trường
			$this->db->where("TRUONG_MA",$truong);
			$this->db->from("truong");
			$truong = $this->db->get()->first_row();
			$arr_data[0]['title'] = @$truong->TRUONG_TEN;
			
			if($kq){
				foreach($kq as $r){
					$arr_data[] = array(
						'sbd' =>$r->NV_SO_BAODANH,
						'hoten' =>$r->NV_HO_TEN,
						'khoi' =>$r->NV_KHOI_THI,
						'mon1' =>$r->NV_DIEM1,
						'mon2' =>$r->NV_DIEM2,
						'mon3' =>$r->NV_DIEM3,
						'tong' =>$r->NV_TONG,
						'khuvuc' =>$r->NV_KHUVUC,
						'ngaysinh' =>$r->NV_NGAYSINH,
						);
				}
			
			}
			die(json_encode($arr_data));
		}
		$data =array();
		$data['urlSearch'] = base_url().'tra-diem.html';
		$data['truong'] = $this->mtruong->gets();
		
		$this->load->view('nv1', $data);
		
	}
	
	function trans_data(){
	$start_time = time();
	ini_set('memory_limit', '5000M');
	$this->load->library('excel');
		$file = 'dataupload/LDA.xlsx';
		$inputFileType = PHPExcel_IOFactory::identify($file);
		$objReader = PHPExcel_IOFactory::createReader($inputFileType);
		$objPHPExcel = $objReader->load($file);
		$sheetData = $objPHPExcel->getActiveSheet()->toArray(null,true,true,true);
		$arr_insert = array();
		unset($sheetData[1]);
		foreach ($sheetData as $data) {
			$arr_insert['NV_MA_TRUONG'] = $data['A'];
			$arr_insert['NV_MA_NGANH'] = '';
			$arr_insert['NV_TEN_NGANH'] = '';
			$arr_insert['NV_KHOI_THI'] = $data['B'];
			$arr_insert['NV_HO_TEN'] = $data['C'];
			$arr_insert['NV_KHUVUC'] = $data['E'];
			$arr_insert['NV_SO_BAODANH'] = $data['F'];
			$arr_insert['NV_DIEM1'] = $data['G']/10;
			$arr_insert['NV_DIEM2'] = $data['H']/10;
			$arr_insert['NV_DIEM3'] = $data['I']/10;
			$arr_insert['NV_TONG'] = $data['J']/10;
			$this->db->insert("nv1",$arr_insert);
		}
		$end = time() - $start_time;
		echo "Time: ".$end." s";
	}
	
	function readExcel(){
	ini_set('memory_limit', '5000M');
	$this->load->library('excel');
		$file = 'dataupload/mau_dhcd.xls';
		$objReader = PHPExcel_IOFactory::createReader('Excel5');
		$objPHPExcel = $objReader->load($file);
		$sheetData = $objPHPExcel->getActiveSheet()->toArray(null,true,true,true);
		echo count($sheetData);
 
		//foreach ($sheetData as $data) {
		//	echo $data['C'] . '<br />';
		//}
		
	
	}
}

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */